/*
 * Copyright (C) 2012 Vinu K.N
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.domainmath.gui.dialog.find_replace;

import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.SearchContext;
import org.fife.ui.rtextarea.SearchEngine;


public class FindDialog extends javax.swing.JDialog {

   
    private  RSyntaxTextArea area;
    public FindDialog(java.awt.Frame parent, boolean modal ,RSyntaxTextArea area) {
        super(parent, modal);
        initComponents();
        this.area=area;
         
         this.findTextField.setText(area.getSelectedText());
         if(findTextField.getText().equals("")) {
            findNxtButton.setEnabled(false);
            findPrevButton.setEnabled(false);
         }
        this.findTextField.getDocument().addDocumentListener(
                new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                
                findNxtButton.setEnabled(true);
                findPrevButton.setEnabled(true);
             
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if(findTextField.getText().equals("")) {
                    findNxtButton.setEnabled(false);
                    findPrevButton.setEnabled(false);
                }
                
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                findNxtButton.setEnabled(true);
                findPrevButton.setEnabled(true);
           }
                    
                });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        closeButton = new javax.swing.JButton();
        findPrevButton = new javax.swing.JButton();
        findNxtButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        findTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        matchCaseCheckBox = new javax.swing.JCheckBox();
        wholeWordsOnlyCheckBox = new javax.swing.JCheckBox();
        regExCheckBox = new javax.swing.JCheckBox();
        markAllComboBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Find");

        closeButton.setMnemonic('C');
        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        findPrevButton.setMnemonic('P');
        findPrevButton.setText("Find Previous");
        findPrevButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findPrevButtonActionPerformed(evt);
            }
        });

        findNxtButton.setMnemonic('N');
        findNxtButton.setText("Find Next");
        findNxtButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findNxtButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Find What:");

        findTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findTextFieldActionPerformed(evt);
            }
        });

        jLabel3.setForeground(java.awt.SystemColor.activeCaption);
        jLabel3.setText("Options");

        matchCaseCheckBox.setMnemonic('M');
        matchCaseCheckBox.setText("Match Case");

        wholeWordsOnlyCheckBox.setText("Match Whole Words Only");

        regExCheckBox.setMnemonic('E');
        regExCheckBox.setText("Regular Expressions");
        regExCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regExCheckBoxActionPerformed(evt);
            }
        });

        markAllComboBox.setMnemonic('L');
        markAllComboBox.setText("Mark All");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSeparator1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(markAllComboBox)
                                    .addComponent(matchCaseCheckBox)
                                    .addComponent(wholeWordsOnlyCheckBox)
                                    .addComponent(regExCheckBox))
                                .addGap(206, 206, 206)))
                        .addGap(129, 129, 129))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(closeButton)
                        .addContainerGap())))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(findTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(findPrevButton)
                        .addComponent(findNxtButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap()))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {closeButton, findNxtButton, findPrevButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(67, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(matchCaseCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(wholeWordsOnlyCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(regExCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(markAllComboBox)
                .addGap(18, 18, 18)
                .addComponent(closeButton)
                .addGap(18, 18, 18))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(findTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(findNxtButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(findPrevButton)
                    .addContainerGap(171, Short.MAX_VALUE)))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {closeButton, findNxtButton, findPrevButton});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void findPrevButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findPrevButtonActionPerformed
        find(false);
    }//GEN-LAST:event_findPrevButtonActionPerformed

    private void findNxtButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findNxtButtonActionPerformed
        find(true);
    }//GEN-LAST:event_findNxtButtonActionPerformed

    private void regExCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regExCheckBoxActionPerformed

   }//GEN-LAST:event_regExCheckBoxActionPerformed

    private void findTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findTextFieldActionPerformed
        this.findNxtButton.doClick();
    }//GEN-LAST:event_findTextFieldActionPerformed

     public void find (boolean forward) {
          SearchContext context = new SearchContext();
      String text = findTextField.getText();
      if (text.length() == 0) {
         return;
      }
      context.setSearchFor(text);
      
      context.setMatchCase(this.matchCaseCheckBox.isSelected());
      context.setRegularExpression(this.regExCheckBox.isSelected());
      context.setSearchForward(forward);
      
      context.setWholeWord(this.wholeWordsOnlyCheckBox.isSelected());

      boolean found = SearchEngine.find(area, context);
      if(this.markAllComboBox.isSelected()) {
          area.markAll(text, this.matchCaseCheckBox.isSelected(), 
                  this.wholeWordsOnlyCheckBox.isSelected(), this.regExCheckBox.isSelected());
      }
      if (!found) {
         JOptionPane.showMessageDialog(this, "Text not found","DomainMath IDE",JOptionPane.INFORMATION_MESSAGE);
      }

    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JButton findNxtButton;
    private javax.swing.JButton findPrevButton;
    private javax.swing.JTextField findTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JCheckBox markAllComboBox;
    private javax.swing.JCheckBox matchCaseCheckBox;
    private javax.swing.JCheckBox regExCheckBox;
    private javax.swing.JCheckBox wholeWordsOnlyCheckBox;
    // End of variables declaration//GEN-END:variables
}
